// pages/evaluate/evaluate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value1: 0,
    value2: 0,
    value3: 0,
    value4: 0,
    imgArr: [], //用户要上传的图片
    checked: true,
    goodsImg: '', //商品图片
    goodsTitle: '', //商品标题
    goodsSpec: '', //商品规格
    goodsPrice: '', //商品价格
    newTagW: 0, //存放（填写新标签）宽度
    evaTag: [{
        tex: '性价比高',
        selected: false
      },
      {
        tex: '外观时尚',
        selected: false
      },
      {
        tex: '功能强大',
        selected: false
      },
      {
        tex: '方便实用',
        selected: false
      },
      {
        tex: '性价比高',
        selected: false
      },
      {
        tex: '性价比高',
        selected: false
      },
    ],
    addMsg: [
      //   {
      //   tex: '',
      //   legh: 0,
      //   fox: false
      // }
    ],
  },
  selectTag(e) { //选择标签
    // console.log(e)
    this.setData({
      ["evaTag[" + e.currentTarget.dataset.id + "].selected"]: !this.data.evaTag[e.currentTarget.dataset.id].selected,
    })
  },
  itemChange(e) { //输入标签（宽度自适应）

    this.setData({
      ['addMsg[' + e.currentTarget.dataset.id + '].tex']: e.detail.value
    })
    // console.log(this.data.addMsg)

    const query = wx.createSelectorQuery()
    const that = this
    query.select('.spans' + [e.currentTarget.dataset.id]).boundingClientRect(function(res) {
      //宽度从另一个元素获取（自适应）
      //输入为空则改变宽度
      if (that.data.addMsg[e.currentTarget.dataset.id].tex == '') {
        that.setData({
          ['addMsg[' + e.currentTarget.dataset.id + '].legh']: that.data.newTagW
        })
      } else {
        that.setData({
          ['addMsg[' + e.currentTarget.dataset.id + '].legh']: res.width
        })
      }

      // console.log(that.data.addMsg[e.currentTarget.dataset.id].legh)
      // console.log(that.data.addMsg[0]);
    }).exec();

  },
  itemFocus(e) { //获得焦点

    let that = this
    // this.setData({
    //   ['addMsg[' + e.currentTarget.dataset.id + '].fox']: true
    // })

    // console.log(this.data.addMsg[e.currentTarget.dataset.id].fox)
    //输入为空则改变宽度
    if (this.data.addMsg[e.currentTarget.dataset.id].tex == '') {
      that.setData({
        ['addMsg[' + e.currentTarget.dataset.id + '].legh']: that.data.newTagW
      })
    }
    // console.log(addMsg)
  },
  itemBlur(e) { //失去焦点
    let that = this
    // console.log(this.data.addMsg[e.currentTarget.dataset.id])
    if (this.data.addMsg[e.currentTarget.dataset.id] != undefined) {

      //输入为空则改变宽度
      if (this.data.addMsg[e.currentTarget.dataset.id].tex == '') {
        that.setData({
          ['addMsg[' + e.currentTarget.dataset.id + '].legh']: that.data.newTagW
        })
      }


      // this.setData({ //失去焦点
      //   ['addMsg[' + e.currentTarget.dataset.id + '].fox']: false
      // })

    }
    // console.log(this.data.addMsg)

  },

  itemAdd(e) { //点击添加标签
    let that = this
    // console.log(this.data.addMsg[this.data.addMsg.length - 1])
    if (this.data.addMsg.length >= 1 && this.data.addMsg[this.data.addMsg.length - 1].tex == '') { //有标签且无内容不能添加
      wx.showToast({
        title: '请先填写当前标签',
        icon: 'none'
      })
    } else {
      let pushData = {
        tex: '',
        legh: that.data.newTagW,
        // fox: false
      }
      this.data.addMsg.push(pushData)
      this.setData({
        addMsg: this.data.addMsg,
      })
      // console.log(e.currentTarget.dataset)
      // this.setData({
      //   ['addMsg[' + e.currentTarget.dataset.length + '].fox']: true
      // })
    }

    // console.log(addArr)
  },
  delBtn(e) {
    let delArr = []
    let that = this
    // console.log(e.currentTarget.dataset.id)
    //删除指定下标的数据

    for (let i = 0; i < that.data.addMsg.length; i++) {
      // console.log(that.data.addMsg[i].tex)
      // that.setData({
      //   ['addMsg[' + i + '].fox']: false
      // })

      if (i != e.currentTarget.dataset.id) {

        delArr.push(that.data.addMsg[i])
      }
    }

    that.setData({
      addMsg: delArr
    })
    // console.log(that.data.addMsg)


  },
  onChange(event) {
    
    this.setData({
      checked: event.detail
    });
    console.log(this.data.checked)
  },
  addImgs(e) { //添加图片
    let that = this
    let puArr = 0

    if (that.data.imgArr.length < 5) { //已上传图片数在5张以内才可执行
      wx.chooseImage({ //本地图片添加进数组中
        count: 5,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          // console.log(tempFilePaths)



          for (let i = 0; i < tempFilePaths.length; i++) {
            // console.log(tempFilePaths[i])

            // puArr = tempFilePaths[i]
            if (that.data.imgArr.length < 5) {
              // console.log(puArr)
              that.data.imgArr.push({
                src: tempFilePaths[i]
              })
              that.setData({
                imgArr: that.data.imgArr
              })
            }
          }
        }
      })
    } else {
      wx.showToast({
        title: '上传图片数量不能超过5张',
        icon: 'none'
      })
    }
  },


  delImg(e) { //删除图片
    console.log(e);
    let that = this
    let delArr = []
    for (let i = 0; i < this.data.imgArr.length; i++) { //除当前index之外的所有图片添加进数组
      if (i != e.currentTarget.dataset.id) {
        delArr.push(that.data.imgArr[i])

      }
    }
    console.log(delArr)
    this.setData({
      imgArr: delArr
    })
  },


  subOuter(e) { //提交
    wx.showToast({
      title: '评价成功',
      duration: 1800,
      success: function() {
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          })
        }, 1800)

      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(JSON.parse(options.goods))
    if (options.goods) { //输出商品信息
      let msg = JSON.parse(options.goods)
      this.setData({
        goodsImg: msg.img,
        goodsTitle: msg.goodsName,
        goodsSpec: msg.speci,
        goodsPrice: msg.price
      })
    }

    // this.setData({
    //   ['addMsg[0].legh']: 50
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this
    wx.createSelectorQuery().select('.defaultSpan').boundingClientRect((ret) => {
      // console.log(ret) //获取当前下标的节点
      that.setData({
        newTagW: ret.width
      })
    }).exec()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})













































































































































































































































